package com.imooc.demo.service.impl;

import com.imooc.demo.dao.Category1Dao;
import com.imooc.demo.dao.Category2Dao;
import com.imooc.demo.entity.Category1;
import com.imooc.demo.entity.Category2;
import com.imooc.demo.service.Category1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Category1ServiceImpl implements Category1Service {
    @Autowired
    private Category1Dao category1Dao;
    @Autowired
    private Category2Dao category2Dao;

    @Override
    public List<Category1> getCategory1List() {
        return category1Dao.queryCategory1();
    }

    @Override
    public Category1 getCategory1ById(int category1Id) {
      //测试用 （1不能除以0）：int a = 1/0;在浏览器输入（直接调用方法getCategory1ById，设置参数为1）：http://localhost:8080/demo/superadmin1/getcategory1byid?category1Id=1
        return category1Dao.getQueryCategory1ById(category1Id);
    }
    //获取RuntimeException并回滚
    @Transactional
    @Override
    public boolean addCategory1(Category1 category1) {
        if(category1.getCategory1Name()!=null && !"".equals(category1.getCategory1Name())){
            try{
                int effectedNum = category1Dao.insertCategory1(category1);
                if(effectedNum>0){
                    return  true;
                }else{
                    throw  new RuntimeException("插入种类信息失败！");
                }
            }catch (Exception e){
                throw new RuntimeException("插入种类信息失败！");
            }
        }else{
            throw new RuntimeException("种类信息不能为空！");
        }
    }

    @Override
    public boolean modifyCategory1(Category1 category1) {
        //空值判断，主要是category1Id不能为空
        if(category1.getCategory1Id()!=null &&category1.getCategory1Id()>0) {
            try {
                //更新种类信息
                int effectedNum = category1Dao.updateCategory1(category1);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("更新种类信息失败！");
                }
            } catch (Exception e) {
                throw new RuntimeException("更新种类信息失败：" + e.toString());
            }
        }else{
            throw new RuntimeException("种类信息不能为空！");
        }

    }

    @Override
    public boolean deleteCategory1(int category1Id) {
        List<Category2> category2By1Id= category2Dao.getCategory2ByCategory1Id( category1Id);
        if (category1Id > 0 &&category2By1Id.size()==0) {
            try {
                //删除种类信息
                int effectedNum = category1Dao.deleteCategory1(category1Id);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("删除种类信息失败！");
                }
            } catch (Exception e) {
                throw new RuntimeException("删除种类信息失败：" + e.toString());
            }
        } else {
            throw new RuntimeException("种类ID不能为空且请查看其是否有下一级分类，有则请先清空其下一级分类！");
        }
    }

    /**
     *  一对多查询
     */
    @Override
    public Category1 selectCategory1ById(int category1Id) {
        return category1Dao.selectCategory1ById( category1Id);
    }

    @Override
    public Category1 AselectCategory1ById(int category1Id) {
        return category1Dao.selectCategory1ById( category1Id);
    }

    @Override
    public Category1 selectCategory2ById(int category2Id) {
        return category1Dao.selectCategory2ById( category2Id);
    }

}
